package cn.chia.front.dao.impl;


import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.front.dao.IGroupPurDAO;
import cn.chia.pojo.TblGroupPurchase;

@Component
public class GroupPurDAOImpl extends HibernateDaoSupport implements IGroupPurDAO {
	@Autowired
	public GroupPurDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	public TblGroupPurchase findGroupById(Integer id) throws Exception {
		return (TblGroupPurchase) getSession().get(TblGroupPurchase.class, id);
	}

	public void addGroup(TblGroupPurchase tblGroupPurchase) throws Exception {
		this.getHibernateTemplate().save(tblGroupPurchase);
	}

	public void updateGroup(TblGroupPurchase tblGroupPurchase) throws Exception {
		this.getHibernateTemplate().update(tblGroupPurchase);
	}

	public void updateState(String id) throws Exception {
		String hql = "UPDATE TblGroupPurchase SET state = 1 WHERE tblItems.itemId ="+id+"";
		Query query = getSession().createQuery(hql);
		query.executeUpdate();	
	}

	
	public List<TblGroupPurchase> findAll() throws Exception {
		String hql = "FROM TblGroupPurchase";
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	public List<TblGroupPurchase> findByState() throws Exception {
		String hql = "FROM TblGroupPurchase WHERE state = 0";
		Query query = getSession().createQuery(hql);
		return query.list();
	}
}
